<template>
  <div v-loading="loading" class="parentStyle">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('基金管理人名称')" prop="name">
            <el-autocomplete
              v-if="!form.id"
              v-model="form.name"
              class="inline-input name"
              :fetch-suggestions="companyQuerySearch"
              :placeholder="$t('管理人全称')"
              :trigger-on-focus="false"
              :maxlength="100"
              @select="companyHandleSelect"
            >
              <template v-if="enableTyc" slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-autocomplete>
            <el-input v-else v-model="form.name" readonly show-word-limit maxlength="100">
              <template v-if="enableTyc" slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('企业统一社会信用代码')" prop="creditCode">
            <el-input v-model="form.company.creditCode" :placeholder="$t('企业统一社会信用代码')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('法定代表人')" prop="legalPerson">
            <el-input v-model="form.company.legalPerson" :placeholder="$t('法定代表人')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('成立时间')" prop="setDate">
            <DatePicker v-model="form.company.setDate" :placeholder="$t('成立时间')" />
          </el-form-item>
        </el-col>
        <!--行业-->
        <Industry ref="industryZj" :buss-data="form.company" />
        <!--地域-->
        <Area ref="cityZj" :buss-data="form.company" />
        <el-col :span="24">
          <el-form-item :label="$t('注册地详细地址')" prop="detailsRegistered">
            <el-input v-model="form.company.detailsRegistered" :placeholder="$t('注册地-详细地址')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本') + $t('金额单位')" prop="capitalRegistered">
            <Money v-model="form.company.capitalRegistered" unit="w" :placeholder="$t('注册资本')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本币种')" prop="currency">
            <dictSelect
              v-model="form.company.currency"
              :clearable="true"
              dictionary="currency"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('公司介绍')" prop="companyProfile">
            <el-input v-model="form.companyProfile" type="textarea" :placeholder="$t('公司介绍')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('股权结构')" prop="ownershipStructure">
            <el-input v-model="form.ownershipStructure" type="textarea" :placeholder="$t('股权结构')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('所获荣誉')" prop="honor">
            <el-input v-model="form.honor" type="textarea" :placeholder="$t('所获荣誉')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('组织架构及人员')" prop="organizationStructure">
            <el-input v-model="form.organizationStructure" type="textarea" :placeholder="$t('组织架构及人员')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('规模及客户分布')" prop="scale">
            <el-input v-model="form.scale" type="textarea" :placeholder="$t('规模及客户分布')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('公司特色及优势')" prop="companyCharacteristic">
            <el-input v-model="form.companyCharacteristic" type="textarea" :placeholder="$t('公司特色及优势')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="gp_security_base" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="gpSecurity:base" :cancel-show="!this.$route.query.id" />
    </div>
  </div>
</template>

<script>
    import { saveGpSecurityBase, getGpSecurityBase, getGpByName } from '@/api/deal/gpSecurity/base/gpSecurityBase'
    // 行业组件
    import Industry from '@/components/Industry'
    // 省市组件
    import Area from '@/components/Area'
    import { gpListByName } from '@/api/fund/gp/gpBase'
    import { Notification } from 'element-ui'
    import { getDealBase } from '@/api/base/deal/dealBase'
    export default {
        components: {
          Industry,
          Area
        },
        data() {
            return {
                // 表单参数
                form: {
                  company: {}
                },
                // 天眼查查询条件
                tycParam: {
                  id: '',
                  name: '',
                  shortName: '',
                  tycId: ''
                },
                // 遮罩层
                loading: false,
                // 是否隐藏其它按钮
                otherBtnHide: false,
                // 表单校验
                rules: {
                  name: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                enableTyc: undefined
            }
        },
        created() {
          this.dealInit()
        },
        mounted() {
          this.$bus.$emit('async_done')
          this.enableTyc = (window.localStorage.getItem('enableTyc') === 'true')
        },
        methods: {
            dealInit() {
              if (this.$route.query.dealType === 'securities') {
                this.loading = true
                getDealBase(this.$route.query.id).then(response => {
                  const data = {
                    id: undefined
                  }
                  data.id = response.data.dealBaseSecurities.fundManager
                  this.init(data)
                })
              }
            },
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getGpSecurityBase(data.id).then(response => {
                      if (response.data != null) {
                        this.form = response.data
                        if (this.form.company === null || this.form.company === undefined) {
                          this.form.company = {}
                        }
                        this.$nextTick(() => {
                          this.$refs.industryZj.initIndustrySecond()
                          this.$refs.cityZj.initCity()
                        })
                      }
                      this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    companyId: undefined,
                    name: undefined,
                    shortName: undefined,
                    company: {
                      industryFirst: undefined,
                      industrySecond: undefined,
                      provinceRegistered: undefined,
                      cityRegistered: undefined
                    },
                    companyProfile: undefined,
                    ownershipStructure: undefined,
                    honor: undefined,
                    organizationStructure: undefined,
                    scale: undefined,
                    companyCharacteristic: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
                this.dealInit()
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveGpSecurityBase(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 标的公司查询
          companyQuerySearch(queryString, cb) {
            gpListByName(queryString).then(res => {
              if (res.code === 200) {
                cb(res.data)
              }
            })
          },
          // 录入管理人简称之后，查询是否已经存在这个管理人
          // 标的公司选择后事件
          companyHandleSelect(item) {
            const name = this.form.name
            if (name === '' || name === undefined) {
              Notification.error({
                title: this.$t('请先输入管理公司全称！！')
              })
              return
            }
            this.tycParam.shortName = item !== undefined ? item.shortName : ''
            if (this.tycParam.shortName === undefined) {
              this.tycParam.shortName = this.form.shortName
            }
            this.tycParam.name = name
            this.tycParam.id = this.form.id
            this.tycParam.type = this.$route.query.type
            getGpByName(this.tycParam).then(res => {
              if (res.data !== null && res.data.id !== null && res.data.id !== '') {
                this.msgError(this.$t('当前管理人已存在，不能重复添加！'))
                this.form.name = ''
                return
              }
              if (item.shortName) {
                this.form.shortName = item.shortName
              }
              this.form.company = res.data.company
              this.form.company.companyShortName = item.shortName
              this.$nextTick(() => {
                if (this.$refs.industryZj?.initIndustrySecond !== undefined) {
                  this.$refs.industryZj.initIndustrySecond()
                }
              })
              this.$nextTick(() => {
                if (this.$refs.cityZj?.initCity !== undefined) {
                  this.$refs.cityZj.initCity()
                }
              })

              // 关联公司ID
              this.form.companyId = res.data.company.id
              this.msgSuccess(this.$t('已获取工商信息！'))
              this.loading = false
            })
          }
        }
    }
</script>
